<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>

<body>
    <script>
        // function getRandomArrayElements(arr, target) {
        //     var shuffled = arr.slice(0),
        //         i = arr.length,
        //         min = i - 4,
        //         temp, index;
        //     while (i-- > min) {
        //         index = Math.floor((i + 1) * Math.random());
        //         temp = shuffled[index];
        //         shuffled[index] = shuffled[i];
        //         shuffled[i] = temp;
        //     }
        //     var zh = 0;
        //     for (var i = 0; i < shuffled.length; i++) {
        //         zh = zh + shuffled[i];
        //     }
        //     if (zh == target) {
        //         return shuffled.slice(min);
        //     }

        // }

        // var items = ['1', '2', '4', '5', '6', '7', '8', '9', '10'];
        // var target = 4;
        // console.log(getRandomArrayElements(items, 20));


        var MyMath = (function() {
            var diff = 0.00000000001;
            var GetSqrt = function(n) {
                    var s = n / 2;
                    while (NotFit(s, n)) {
                        s = ((n / s) + s) / 2;
                    }
                    return s;
                },
                NotFit = function(num, n) {
                    var r = num * num;
                    var d = Abs(n - Abs(r));
                    if (d > diff) {
                        return true;
                    }
                    return false;
                },
                Abs = function(a) {
                    if (a >= 0) return a;
                    else return -a;
                };

            return {
                sqrt: GetSqrt,
                abs: Abs
            };
        })();



        console.log(MyMath.sqrt(9));
    </script>
</body>

</html>